package com.medical.app.service.impl;

import com.medical.app.common.dto.BeHospitalizedDto;
import com.medical.app.common.dto.HospitalizedRechargeDto;
import com.medical.app.common.dto.MedicalRecordDto;
import com.medical.app.entity.BeHospitalized;
import com.medical.app.entity.MedicalRecord;
import com.medical.app.mapper.BeHospitalizedMapper;
import com.medical.app.mapper.MedicalRecordMapper;
import com.medical.app.service.HospitalizedService;
import com.qf.medical.ex.ServiceException;
import com.qf.medical.response.ResponseCode;
import com.qf.medical.utils.BeanConvertUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class HospitalizedServiceImpl implements HospitalizedService {
    @Resource
    private BeHospitalizedMapper beHospitalizedMapper;
    @Resource
    private MedicalRecordMapper medicalRecordMapper;

    @Override
    public List<BeHospitalizedDto> searchHospitalized(Long patientId) {
        List<BeHospitalized> beHospitalized = beHospitalizedMapper.selectByPatientId(patientId);
        if (ObjectUtils.isEmpty(beHospitalized)){
            throw new ServiceException(ResponseCode.HOSPITALIZED_SELECT_ERROR);
        }
        return BeanConvertUtils.convertListTo(beHospitalized,BeHospitalizedDto::new);
    }

    @Override
    public HospitalizedRechargeDto hospitalizedRecharge(Long patientId) {
        HospitalizedRechargeDto hospitalizedRechargeDto = beHospitalizedMapper.selectMessage(patientId);
        if (ObjectUtils.isEmpty(hospitalizedRechargeDto)){
            throw new ServiceException(ResponseCode.HOSPITALIZED_SELECT_ERROR);
        }
        return hospitalizedRechargeDto;
    }

    @Override
    public List<HospitalizedRechargeDto> getHospitalList(HospitalizedRechargeDto hospitalizedRechargeDto) {
        Long patientId = hospitalizedRechargeDto.getPatientDto().getPatientId();
        Date startDate = hospitalizedRechargeDto.getStartDate();
        Date endDate = hospitalizedRechargeDto.getEndDate();
        List<HospitalizedRechargeDto> list = beHospitalizedMapper.selectHospitalizedList(patientId,startDate,endDate);
        if (ObjectUtils.isEmpty(list)){
            throw new ServiceException(ResponseCode.HOSPITALIZED_SELECT_ERROR);
        }
        return list;
    }

    @Override
    public List<MedicalRecordDto> getMedicalRecord(Long patientId) {
        List<MedicalRecord> list = medicalRecordMapper.selectByPatientId(patientId);
        if (ObjectUtils.isEmpty(list)){
            throw new ServiceException(ResponseCode.MEDICAL_RECORD_SELECT_ERROR);
        }
        return BeanConvertUtils.convertListTo(list,MedicalRecordDto::new);
    }

    @Override
    public MedicalRecordDto detailMedicalRecord(Integer recordId) {
        MedicalRecord medicalRecord = medicalRecordMapper.selectById(recordId);
        if (ObjectUtils.isEmpty(medicalRecord)){
            throw new ServiceException(ResponseCode.MEDICAL_RECORD_SELECT_ERROR);
        }
        return BeanConvertUtils.convertTo(medicalRecord,MedicalRecordDto::new);
    }
}
